package com.cf.controller;

import cn.hutool.crypto.SecureUtil;
import com.cf.domain.ResponseResult;
import com.cf.domain.dto.UserDTO;
import com.cf.domain.vo.UserVO;
import com.cf.enums.UserRole;
import com.cf.exception.MyException;
import com.cf.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequiredArgsConstructor
public class LoginController {

    private final IUserService userService;

    /**
     * 登陆
     * @param phone
     * @param password
     * @param session
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public ResponseResult login(@RequestParam("phone") String phone,
                                @RequestParam("password") String password,
                                HttpSession session){
        UserDTO userDTO = new UserDTO();
        userDTO.setPassword(password);
        userDTO.setPhone(phone);
        UserVO userVO = userService.login(userDTO);
        session.setAttribute("user", userVO);
        return ResponseResult.sucess(null);
    }

    /**
     * 注册
     * @param name
     * @param phone
     * @param area
     * @param password
     * @param session
     * @return
     */
    @PostMapping("/register")
    @ResponseBody
    public ResponseResult register(@RequestParam("name") String name,
                           @RequestParam("phone") String phone,
                           @RequestParam("email") String email,
                           @RequestParam("area") Integer area,
                           @RequestParam("type") Integer type,
                           @RequestParam("source") Integer source,
                           @RequestParam("password") String password, HttpSession session){
        try {
            UserDTO userDTO = new UserDTO();
            userDTO.setName(name);
            userDTO.setPassword(password);
            userDTO.setPhone(phone);
            userDTO.setEmail(email);
            userDTO.setArea(area);
            userDTO.setType(type);
            userDTO.setSource(source);
            //注册为普通用户
            userDTO.setUserRole(UserRole.USER.getId());
            UserVO userVO = userService.register(userDTO);
            session.setAttribute("user", userVO);
            return ResponseResult.sucess(null);
        } catch (MyException e) {
            return ResponseResult.fail(e.getMessage());
        }
    }


    @PostMapping("/updatePwd")
    @ResponseBody
    public ResponseResult updatePwd(@RequestParam("password") String password,
                                    @RequestParam("newPwd") String newPwd,
                                    @RequestParam("newPwdCopy") String newPwdCopy, HttpSession session){
        try {
            UserVO userVO = (UserVO) session.getAttribute("user");
            UserDTO userDTO = new UserDTO();
            userDTO.setId(userVO.getId());
            userDTO.setPassword(SecureUtil.md5(password));
            userService.updatePwd(userDTO, newPwd);
            return ResponseResult.sucess(null);
        }catch (MyException e){
            return ResponseResult.fail(e.getMessage());
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "home/login";
    }

}
